﻿#include "senddirectiondlg.h"

#include "ui_senddirectiondlg.h"

#include <QtCharts/QChartView>
#include <QtCharts/QPolarChart>

#include <QtCharts/QSplineSeries>
#include <QtCharts/QValueAxis>

QT_CHARTS_USE_NAMESPACE


SendDirectionDlg::SendDirectionDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SendDirectionDlg)
{
    ui->setupUi(this);
    initForm ();
    initButton ();
    initPlorGraph ();
}

SendDirectionDlg::SendDirectionDlg(QVector<double> m1, QVector<double> m2, QWidget *parent):
    QDialog(parent),
    ui(new Ui::SendDirectionDlg)
{
    ui->setupUi(this);
    initForm ();
    initButton ();
}

SendDirectionDlg::~SendDirectionDlg()
{
    delete ui;
}

void SendDirectionDlg::initForm()
{
    m_totleHeight = 600;                //界面总高度
    m_Width = 700;                     //界面总宽度
    m_titleHeight = 40;                 //标题栏高度

    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint);
    this->setAttribute(Qt::WA_TranslucentBackground,true);
    this->resize(m_Width,m_totleHeight);

    this->setProperty("canMove", QVariant(true));

    QDesktopWidget *desktop = QApplication::desktop();
    this->move((desktop->width()-this->width())/2 , (desktop->height()-this->height())/2);
    this->setWindowOpacity (1);
}

void SendDirectionDlg::initPlorGraph()
{
    const qreal angularMin = 0;
    const qreal angularMax = 360;

    const qreal radialMin = 0;
    const qreal radialMax = 100;

    //曲线
    QSplineSeries *series2 = new QSplineSeries();
    series2->setName("发射指向性");
//    for (int i = angularMin; i <= angularMax; i += 10)
//        series2->append(i, (i / radialMax) * radialMax);
    series2->append (0,100);
    series2->append (10,99);
    series2->append (20,98);
    series2->append (30,96);
    series2->append (40,93);
    series2->append (50,88);
    series2->append (60,82);
    series2->append (70,77);
    series2->append (80,74);
    series2->append (90,70);

    series2->append (100,67);
    series2->append (110,63);
    series2->append (120,56);
    series2->append (130,49);
    series2->append (140,42);
    series2->append (150,45);
    series2->append (160,55);
    series2->append (170,65);
    series2->append (180,68);

    series2->append (190,65);
    series2->append (200,55);
    series2->append (210,45);
    series2->append (220,42);
    series2->append (230,49);
    series2->append (240,56);
    series2->append (250,63);
    series2->append (260,67);
    series2->append (270,72);

    series2->append (280,74);
    series2->append (290,77);
    series2->append (300,82);
    series2->append (310,88);
    series2->append (320,93);
    series2->append (330,96);
    series2->append (340,98);
    series2->append (350,99);
    series2->append (360,100);

    //图表
    QPolarChart *chart = new QPolarChart();
    chart->addSeries(series2);

    QValueAxis *angularAxis = new QValueAxis();
    angularAxis->setTickCount(9); // First and last ticks are co-located on 0/360 angle.
    angularAxis->setLabelFormat("%.1f");
    angularAxis->setShadesVisible(true);
    angularAxis->setShadesBrush(QBrush(QColor(249, 249, 255)));
    chart->addAxis(angularAxis, QPolarChart::PolarOrientationAngular);

    QValueAxis *radialAxis = new QValueAxis();
    radialAxis->setTickCount(9);
    radialAxis->setLabelFormat("%d");
    chart->addAxis(radialAxis, QPolarChart::PolarOrientationRadial);

    series2->attachAxis(radialAxis);
    series2->attachAxis(angularAxis);

    radialAxis->setRange(radialMin, radialMax);
    angularAxis->setRange(angularMin, angularMax);

    //加入画布
    QChartView *chartView = new QChartView();
    chartView->setChart(chart);
    chartView->setRenderHint(QPainter::Antialiasing);

    ui->hLayout->addWidget (chartView);

}

void SendDirectionDlg::initButton()
{
    btnClose =new QPushButton(this);
    btnMin = new QPushButton(this);

    //QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    QIcon close_buttonIcon(":/image/btn_close_normal.png");
    QIcon min_buttonIcon(":/image/btn_small_normal.png");

    btnClose->setIcon(close_buttonIcon);
    btnMin->setIcon(min_buttonIcon);
    btnClose->setGeometry(m_Width-40,9,30,30);
    btnMin->setGeometry (m_Width-70,9,30,30);
    btnClose->setStyleSheet("background-color:transparent;");
    btnMin->setStyleSheet("background-color:transparent;");
    //btnSetup->setStyleSheet("color:#7A95B0;background-color:transparent;");

    connect(btnClose,SIGNAL(clicked()),this,SLOT(btnMenu_Close_clicked()));
    connect(btnMin,SIGNAL (clicked()),this,SLOT(btnMenu_Min_clicked()));
}

void SendDirectionDlg::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setBrush(QBrush(QColor("#8A8688")));
    painter.setPen(QPen(Qt::transparent));
    painter.drawRect(0,0,m_Width,m_titleHeight);

    painter.setPen(QPen("#000000"));
    painter.setBrush(QBrush(QColor("#FFFFFF")));
    painter.drawRect(0,m_titleHeight,m_Width-1,m_totleHeight-m_titleHeight-1);

    QPixmap pixmap;
    pixmap.load (":/image/FstLogo.ico");
    painter.drawPixmap (10,8,24,24,pixmap);
}

void SendDirectionDlg::btnMenu_Close_clicked()
{
    this->close ();
}

void SendDirectionDlg::btnMenu_Min_clicked()
{
    this->showMinimized ();
}
